package com.samapp.utils.json;

import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

/**
 * 封装JSON组合工具类
 */
public class ReturnJson {

	private final static String successCode = "000"; // 成功状态码value
	private final static String message = "desc"; // 信息key
	private final static String data = "categories"; // 数据key
	private final static String returnTitle = "result"; // 状态码key

	/**
	 * 返回json格式字符串 状态码和信息
	 * 
	 * @param code
	 * @param messages
	 * @return
	 */
	public static String getMessageJson(String code, String messages) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append(returnTitle);
		buffer.append("\":\"");
		buffer.append(code);
		buffer.append("\",\"");
		buffer.append(message);
		buffer.append("\":\"");
		buffer.append(messages);
		buffer.append("\"}");
		return buffer.toString();
	}

	/**
	 * 有INFO数组
	 * 
	 * @param object
	 *            集合
	 */
	public static String getObjectJson(Object object, String messages) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append(returnTitle);
		buffer.append("\":\"");
		buffer.append(successCode);
		buffer.append("\",\"");
		buffer.append(message);
		buffer.append("\":\"");
		buffer.append(messages);
		buffer.append("\",\"");
		buffer.append(data);
		buffer.append("\":");
		buffer.append(new Gson().toJson(object));
		buffer.append("}");
		return buffer.toString();
	}

	public static String returnObjectToJson(String result, String desc, String info, Object object) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append("result");
		buffer.append("\":\"");
		buffer.append(result);
		buffer.append("\",\"");
		buffer.append("desc");
		buffer.append("\":\"");
		buffer.append(desc);
		buffer.append("\",\"");
		buffer.append(info);
		buffer.append("\":");
		buffer.append(new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create().toJson(object));
		buffer.append("}");
		return buffer.toString();
	}

	public static String returnObjectToJson(String result, String desc, String otherKey, String otherValue, String info, Object object) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append("result");
		buffer.append("\":\"");
		buffer.append(result);
		buffer.append("\",\"");
		buffer.append(otherKey);
		buffer.append("\":\"");
		buffer.append(otherValue);
		buffer.append("\",\"");
		buffer.append("desc");
		buffer.append("\":\"");
		buffer.append(desc);
		buffer.append("\",\"");
		buffer.append(info);
		buffer.append("\":");
		buffer.append(new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create().toJson(object));
		buffer.append("}");
		return buffer.toString();
	}

	public static String returnObjectsToJson(String result, String desc, String[] infos, Object[] objects) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append("result");
		buffer.append("\":\"");
		buffer.append(result);
		buffer.append("\",\"");
		buffer.append("desc");
		buffer.append("\":\"");
		buffer.append(desc);
		buffer.append("\"");

		for (int i = 0; i < infos.length; i++) {
			String info = infos[i];
			Object object = objects[i];

			buffer.append(",\"");
			buffer.append(info);
			buffer.append("\":");
			buffer.append(new GsonBuilder().registerTypeAdapterFactory(new NullStringToEmptyAdapterFactory()).create().toJson(object));
		}

		buffer.append("}");
		return buffer.toString();
	}

	public static String returnStringToJson(String result, String desc, String info, String json) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append("result");
		buffer.append("\":\"");
		buffer.append(result);
		buffer.append("\",\"");
		buffer.append("desc");
		buffer.append("\":\"");
		buffer.append(desc);
		buffer.append("\",\"");
		buffer.append(info);
		buffer.append("\":");
		buffer.append(json);
		buffer.append("}");
		return buffer.toString();
	}

	public static String returnMsgToJson(String result, String desc) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append("result");
		buffer.append("\":\"");
		buffer.append(result);
		buffer.append("\",\"");
		buffer.append("desc");
		buffer.append("\":\"");
		buffer.append(desc);
		buffer.append("\"}");
		return buffer.toString();
	}

	/**
	 * 没有INFO数组
	 * 
	 * @param object
	 *            集合
	 */
	public static String getObjectJsonOfNoInfo(Object object, String messages) {

		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append(returnTitle);
		buffer.append("\":\"");
		buffer.append(successCode);
		buffer.append("\",\"");
		buffer.append(message);
		buffer.append("\":\"");
		buffer.append(messages);
		buffer.append("\",");
		String tm = new Gson().toJson(object);
		buffer.append(tm.substring(1, tm.length() - 1));
		buffer.append("}");
		return buffer.toString();
	}

	/**
	 * 没有INFO数组 成功,同级的数据设置
	 * 
	 * @return
	 */
	public static String getObjectNoInFoJson(Map<String, String> map, String messages) {
		map.put(returnTitle, successCode);
		map.put(message, messages);
		return new Gson().toJson(map);
	}

	/**
	 * 插入json数组 省市区JSON使用
	 * 
	 * @param jsonStr
	 *            json字符串
	 */
	public static String getLocalDataJson(String jsonStr) {
		StringBuffer buffer = new StringBuffer();
		buffer.append("{\"");
		buffer.append(returnTitle);
		buffer.append("\":\"");
		buffer.append(successCode);
		buffer.append("\",\"");
		buffer.append(data);
		buffer.append("\":");
		buffer.append(jsonStr);
		buffer.append("}");
		return buffer.toString();
	}

	/**
	 * 返回给h5的json需要使用这个方法进行回调处理 原因是h5的跨域问题，这里的：jsonpCallback
	 * 是h5回调函数名称。是一个约定，并非一个限定
	 * 
	 * @param jsonData
	 *            封装好的json数据
	 * @return
	 * 
	 *         例子： return
	 *         ReturnJson.jsonCallBack(clientManagerService.getClientInfo
	 *         (jsonData));
	 * 
	 */
	public static String jsonCallBack(String jsonData) {
		return "jsonpCallback(" + jsonData + ")";
	}

	/**
	 * 为jsonp请求指定一个回调函数名。这个值将用来取代jQuery自动生成的随机函数名。这主要用来让jQuery生成度独特的函数名，
	 * 这样管理请求更容易，也能方便地提供回调函数和错误处理。你也可以在想让浏览器缓存GET请求的时候，指定这个回调函数名。 区分返回的数据
	 * 
	 * @param jsonData
	 * @return
	 */
	public static String jsonCallBack2(String jsonData) {
		return "jsonpCallback2(" + jsonData + ")";
	}
}
